﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoccaStore.Data;
using RoccaStore.Utils;

namespace RoccaStore.Services
{
    public class SlugService : ServiceBase<ISlugRepository>, ISlugService
    {
        public Slug GetByID(int id)
        {
            return Repository.GetByID(id);
        }

        public Slug GetByName(string name)
        {
            return Repository.GetByName(name);
        }

        public Slug CreateSlug(string title, int? fileId)
        {
            var slug = new Slug()
            {
                Title = title,
                Name = string.Empty,
                Type = Enums.SlugType.File,
                FileID = fileId
            };

            Save(slug);

            // geneate slug
            slug.Name = HashUtils.Encode(slug.ID);
            slug.Key = SecurityUtils.HashEncrypt(slug.ID.ToString());

            Save(slug);

            return slug;
        }

        public int Save(Slug slug)
        {
            return Repository.Save(slug);
        }

        public void Delete(int id)
        {
            Repository.Delete(id);
        }

        public List<Slug> GetByParent(string name)
        {
            return Repository.GetByParent(name);
        }

        public List<Slug> GetByParent(int id)
        {
            return Repository.GetByParent(id);
        }
    }
}
